<template>
  <div>
    <!-- 1.定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>病人管理</el-breadcrumb-item>
      <el-breadcrumb-item>病人信息</el-breadcrumb-item>
    </el-breadcrumb>


<el-card class="box-card" >
  <el-row :gutter="20">
    <el-col :span="12">
      <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable  @clear="getPatientList()">
          <el-button slot="append" icon="el-icon-search" @click="getPatientList()"></el-button>
      </el-input>
      </el-col>
    <el-col :span="6">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/registration' }">
          <el-button type="primary" >挂号</el-button>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
  </el-row>

        <!-- :data 就是 v-for prop就是要绑定的数据-->
       <!-- 用户数据表格展现  border 边框线属性
              type="index" 表格的索引值
       -->
       <el-table :data="patientList" border :row-class-name="tableRowClassName">
           <el-table-column type="index" label="序号"></el-table-column>
           <el-table-column prop="username" label="姓名"></el-table-column>
           <el-table-column prop="sex" label="性别"></el-table-column>
           <el-table-column prop="age" label="年龄"></el-table-column>
           <el-table-column prop="phone" label="手机号"></el-table-column>
           <el-table-column prop="hospital" label="科室"></el-table-column>
           <el-table-column prop="doctors" label="医生"></el-table-column>
           <el-table-column prop="conditions" label="症状详情"></el-table-column>
           <el-table-column label="操作">
             <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="small" @click="updatePatientBtn(scope.row)"></el-button>
                <el-button type="danger" icon="el-icon-delete" size="small" @click="deletePatient(scope.row)"></el-button>
             </template>
           </el-table-column>
       </el-table>
   </el-card>
    <el-pagination
         @size-change="handleSizeChange"
         @current-change="handleCurrentChange"
         :current-page="queryInfo.pageNum"
         :page-sizes="[5, 10, 20,40]"
         :page-size="queryInfo.pageSize"
         layout="total, sizes, prev, pager, next, jumper"
         :total="total">
    </el-pagination>

    <!-- 添加新增分类对话框 -->
<!--    <el-dialog title="添加病人" :visible.sync="addPatientVisible" width="50%" @close="closeAddPatientDialog" >

      <el-form :model="itemPatientForm" :rules="rules"  ref="itemPatientFormRef" label-width="100px" >
        <el-form-item label="病人姓名:" prop="username">
          <el-input v-model="itemPatientForm.username" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="itemPatientForm.sex" placeholder="性别">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄:" prop="age">
          <el-input type="number" placeholder="年龄" v-model="itemPatientForm.age"  :min="0" :max="150"></el-input>
        </el-form-item>
      <el-form-item label="信息:" prop="conditions">
        <el-input v-model="itemPatientForm.conditions" placeholder="信息"></el-input>
      </el-form-item>
      </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button @click="addPatientVisible = false">取 消</el-button>
        <el-button type="primary" @click="addPatient()">确 定</el-button>
        </span>

</el-dialog> -->
<!-- 修改信息对话框-->
    <el-dialog title="修改信息" :visible.sync="updataPatientVisible" width="50%" @close="closeAddPatientDialog" >

      <el-form :model="updataPatientForm" :rules="rules"  ref="itemPatientForm" label-width="100px" >
        <el-form-item label="病人姓名:" prop="username" >
          <el-input v-model="updataPatientForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="性别:" prop="sex">
          <el-select v-model="updataPatientForm.sex" placeholder="性别">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄:" prop="age">
          <el-input type="number" placeholder="年龄" v-model="updataPatientForm.age"  :min="0" :max="150"></el-input>
        </el-form-item>
        <el-form-item label="手机号:" prop="phone" >
          <el-input v-model="updataPatientForm.phone" ></el-input>
        </el-form-item>
        <el-form-item label="科室:" prop="hospital"  >
        <el-cascader ref="refSubCat" v-model="selectedKeys" :props="props" :options="parentHospitalList"
          @change="parentHospitalChange" :show-all-levels="false"></el-cascader>
         </el-form-item>
         <el-form-item label="医生:" prop="doctors">
           <template slot-scope="scope">
           <el-select v-model="updataPatientForm.doctors" clearable placeholder="请先选择科室" @click.native="getDoctor()">
               <el-option
                 v-for="item in DoctorList"
                 :key="item.name"
                 :label="item.name"
                 :value="item.name"
                 >
               </el-option>
             </el-select>
             </template>
         </el-form-item>
      <el-form-item label="信息:" prop="conditions">
        <el-input v-model="updataPatientForm.conditions"></el-input>
      </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updataPatientVisible = false">取 消</el-button>
        <el-button type="primary" @click="updataPatient()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default{
    data(){
      const checkPhone = (rule, value, callback) => {

        //定义校验手机号的正则语法
        const phoneRege = /^1[34578][0-9]{9}$/
        if (phoneRege.test (value)) {

          return callback()
        }
        callback(new Error('请填写正确的手机号'))

      }
      return{
        // addPatientVisible: false,
        updataPatientVisible: false,
        queryInfo: {
          query: '',
          pageNum: 1,
          pageSize: 5
        },
        total: 0,
        patientList: [],
        itemPatientForm:{},
        updataPatientForm:{},
        rules:{
            username:[
              /* required是否为必填项 message:提示信息 trigger触发条件*/
              {required: true,message: "请输入姓名",trigger: 'blur'}
            ],
            phone:[
              { required: true, message: '请输入手机号', trigger: 'blur' },
              { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' },
              { validator: checkPhone , trigger: 'blur' }
            ],
        },
        props: {
          //定义子节点菜单展开方式
          expandTrigger: "click",
          value: "id", //选中数据的value值
          label: "name", //选中数据展现名称
          children: "children", //自选项数据

          // checkStrictly: true
        },
        parentHospitalList: [],
        DoctorForm: {
          name: '', //定义商品分类名称
          parentId: 0, //默认父级ID=0
          level: 1 //默认是一级菜单
        },
        DoctorList:[],
        value:[],
        selectedKeys:[]
    }
   },
    methods:{

      async getDoctor(){
        const {data:result} = await this.$http.get(`/doctors/findDoctorList/${this.updataPatientForm.hospital}`)
        if (result.status !== 200) return this.$message.error("获取医生列表失败!!")
        this.DoctorList = result.data
      },
      async getHospitalList(){
        const {data:result} = await this.$http.get(`/hospital/findHospitalList/3`)
        if (result.status !== 200) return this.$message.error("获取科室列表失败!!")
        this.parentHospitalList = result.data
      },
      parentHospitalChange() {
        this.updataPatientForm.hospital = this.$refs["refSubCat"].getCheckedNodes()[0].label
        console.log(this.updataPatientForm.hospital)
        // this.itemPatientForm.hospital = this.$refs["refSubCat"].getCheckedNodes()[0].label
      },
      async deletePatient(Patient){
        const result =  await this.$confirm('此操作将永久删除 , 是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                          }).catch(error => error)
                  //如果确认  confirm  如果取消 cancel
          if(result !== 'confirm'){
              return this.$message.info("删除取消")
         }

         const {data:result1} = await this.$http.delete(`/patient/deletePatient/${Patient.id}`)
         this.$message.success("删除成功")
         this.getPatientList();
      },
      updatePatientBtn(Patient){
        this.getHospitalList()
        this.updataPatientForm = Patient;
        this.updataPatientVisible = true;

      },
      async updataPatient(){
          const { data: result } = await this.$http.put("/patient/updataPatient",this.updataPatientForm)
          if (result.status !== 200) return this.$message.error("修改状态失败")
          this.$message.success("状态修改成功")
          this.updataPatientVisible = false
      },
      async getPatientList(){
        const {data: result} = await this.$http.get('/patient/list',{
           params: this.queryInfo
        })
        if(result.status !== 200) return this.$message.error("病人列表查询失败")
        this.patientList = result.data.rows
        this.total = result.data.total
      },

/*      async addPatient(){
        this.$refs.itemPatientFormRef.validate(async valid => {
          //如果校验失败 则停止数据
          if(!valid) return
          const{data: result} = await this.$http.post(`/patient/addPatient`,this.itemPatientForm)
           if(result.status !== 200) return this.$message.error("数据添加失败")
           this.getPatientList();
           this.addPatientVisible = false;
          this.itemPatientForm = {}
        })
      }, */
      addPatientBtn(){
        this.getHospitalList();
        this.addPatientVisible = true
      },
      handleSizeChange(pageSize){

        this.queryInfo.pageSize = pageSize
        this.getPatientrList()
      },
      handleCurrentChange(pageNum){

        this.queryInfo.pageNum = pageNum
        this.getPatientList()
      },
      //当点击关闭按钮时,应该重置整个表单
      closeAddPatientDialog() {
        this.itemPatientForm = {}
      },
      tableRowClassName({row, rowIndex}) {
              if (rowIndex === 1) {
                return 'warning-row';
              } else if (rowIndex === 3) {
                return 'success-row';
              }
              return '';
            }
    },
    mounted(){
      this.getPatientList()
    }
  }
</script>

<style>
  input::-webkit-outer-spin-button,
      input::-webkit-inner-spin-button {
          -webkit-appearance: none;
      }
      input[type="number"]{
          -moz-appearance: textfield;
      }
.el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }

</style>
